/*************************************************************
 * File: BaseEntity.java
 * Copyright (c) 2012
 * Author: Huang Shaobin
 * Email: Shaobin.Software@gmail.com Shaobin_Software@163.com 
 * Date: Feb 3, 2013
 * Description: 
 ************************************************************/

package snow.biz.base.model;

import java.io.Serializable;
import java.util.Date;

import org.apache.commons.lang.ObjectUtils;

import snow.biz.system.model.User;
import snow.core.enums.Status;
import snow.core.persistence.Entity;
import snow.core.toolkit.format.Formatter;

/**
 * 基础实体
 * 
 * @author Shaobin.Software@gmail.com Shaobin_Software@163.com
 * @version 1.0
 */
public class BaseEntity<ID extends Serializable> implements Entity<ID> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6280314879473949077L;

	public static boolean validEntity(BaseEntity<?> entity) {
		return entity != null && Status.SUCCESS.equals(entity.getStatus());
	}

	private ID id; // 自然主键，一般由数据库自动生成
	private User createUser; // 记录创建人
	private Date createTime; // 记录创建时间
	private User modifyUser; // 记录最近修改人
	private Date modifyTime; // 记录最近修改时间
	private Status status; // 记录状态

	protected BaseEntity() {
	}

	public BaseEntity(User createUser) {
		this(createUser, Status.FAILURE);
	}

	public BaseEntity(User createUser, Status status) {
		final Date currentTime = new Date();
		this.createTime = currentTime;
		this.createUser = createUser;
		this.modifyTime = currentTime;
		this.modifyUser = createUser;
		this.status = status;
	}

	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj != null && getClass().isAssignableFrom(obj.getClass())) {
			return ObjectUtils.equals(getId(), ((BaseEntity<?>) obj).getId());
		}
		return false;
	}

	@Formatter(format = "yyyy-MM-dd HH:mm:ss")
	public Date getCreateTime() {
		return this.createTime;
	}

	@Formatter(serialize = false)
	public User getCreateUser() {
		return this.createUser;
	}

	public ID getId() {
		return this.id;
	}

	@Formatter(format = "yyyy-MM-dd HH:mm:ss")
	public Date getModifyTime() {
		return this.modifyTime;
	}

	@Formatter(serialize = false)
	public User getModifyUser() {
		return this.modifyUser;
	}

	public Status getStatus() {
		return this.status;
	}

	public int hashCode() {
		return ObjectUtils.hashCode(getId());
	}

	public void setCreateTime(Date createTime) {
	}

	public void setCreateUser(User createUser) {
	}

	public void setId(ID id) {
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}

	public void setModifyUser(User modifyUser) {
		this.modifyUser = modifyUser;
	}

	public void setStatus(Status status) {
		this.status = status;
	}

	public String toString() {
		return getClass().getName() + '@' + getId();
	}

}
